﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;

namespace CoreWeb.Serialization
{
    public class JsonHelper
    {
        /// <summary>
        /// 序列化对象对json字符串
        /// </summary>
        /// <param name="value">对象值</param>
        /// <param name="type">对象类型</param>
        /// <returns></returns>
        public static string SerializeToString(object value, Type type)
        {
            return JsonConvert.SerializeObject(value, type, JsonSettings.JsonSerializerSettings);
        }

        /// <summary>
        /// 序列化对象对json字符串
        /// </summary>
        /// <typeparam name="T">对象类型参数</typeparam>
        /// <param name="value">对象值</param>
        /// <returns></returns>
        public static string SerializeToString(object value)
        {
            return JsonConvert.SerializeObject(value, JsonSettings.JsonSerializerSettings);
        }

        /// <summary>
        /// 反序列化json字符串到对象
        /// </summary>
        /// <typeparam name="T">对象类型参数</typeparam>
        /// <param name="json">json字符串</param>
        /// <returns></returns>
        public static T DeserializeFromString<T>(string json)
        {
            return JsonConvert.DeserializeObject<T>(json, JsonSettings.JsonSerializerSettings);
        }

        /// <summary>
        /// 反序列化json字符串到对象
        /// </summary>
        /// <param name="json">json字符串</param>
        /// <param name="type">对象类型</param>
        /// <returns></returns>
        public static object DeserializeFromString(string json, Type type)
        {
            return JsonConvert.DeserializeObject(json, type, JsonSettings.JsonSerializerSettings);
        }
    }
}
